package com.ayf.payment.game.api.service.payment.huandong.util;

import com.ayf.payment.game.api.utils.MD5Util;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

public class HuanDongSignUtil {


    public static String getSign(String traceId, String methodName, Map<String, String> reqParam, String key) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : reqParam.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String signOriginStr = sb.toString();
        signOriginStr=signOriginStr.substring(0,signOriginStr.length()-1).concat(key);
        LogPortal.info("{} {} 签名原文:{}",traceId,methodName,signOriginStr);
        String sign = MD5Util.strToMd5Utf8(signOriginStr);
        if (StringUtils.isEmpty(sign)) {
            return null;
        }
        LogPortal.info("{} {} 实际签名:{}",traceId,methodName,sign);
        return sign;
    }

    public static String getPaySign(String traceId, String methodName, Map<String, String> reqParam, String key) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : reqParam.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String signOriginStr = sb.toString();
        signOriginStr=signOriginStr.concat("key=").concat(key);
        LogPortal.info("{} {} 签名原文:{}",traceId,methodName,signOriginStr);
        String sign = MD5Util.strToMd5Utf8(signOriginStr);
        if (StringUtils.isEmpty(sign)) {
            return null;
        }
        sign = sign.toUpperCase();
        LogPortal.info("{} {} 实际签名:{}",traceId,methodName,sign);
        return sign;
    }

    public static String getPayQuerySign(String traceId, String methodName, Map<String, String> reqParam, String key) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : reqParam.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String signOriginStr = sb.toString();
        signOriginStr=signOriginStr.substring(0,signOriginStr.length()-1).concat(key);
        LogPortal.info("{} {} 签名原文:{}",traceId,methodName,signOriginStr);
        String sign = MD5Util.strToMd5Utf8(signOriginStr);
        if (StringUtils.isEmpty(sign)) {
            return null;
        }
        sign = sign.toUpperCase();
        LogPortal.info("{} {} 实际签名:{}",traceId,methodName,sign);
        return sign;
    }
}
